const fs = require('fs-extra');
const path = require('path');
const { exec } = require('pkg');

const OUTPUT_PATH = path.join(__dirname, '../output');
const RELEASE_PATH = path.join(OUTPUT_PATH, 'dist');
const ORIGIN_CHROME_PATH = path.join(__dirname, '../chrome-win64');
const RELEASE_CHROME_PATH = path.join(RELEASE_PATH, './chrome-win64');

const ORIGIN_CONFIG_PATH = path.join(__dirname, '../config');
const ORIGIN_CSV_PATH = path.join(__dirname, '../csv');
const ORIGIN_DB_PATH = path.join(__dirname, '../db');

const RELEASE_CONFIG_PATH = path.join(RELEASE_PATH, './config');
const RELEASE_CSV_PATH = path.join(RELEASE_PATH, './csv');
const RELEASE_DB_PATH = path.join(RELEASE_PATH, './db');
const RELEASE_LOGS_PATH = path.join(RELEASE_PATH, './logs');



const Builder = {};

Builder.build = async function build() {
	for (const dir of [RELEASE_CONFIG_PATH, RELEASE_CSV_PATH, RELEASE_DB_PATH, RELEASE_LOGS_PATH]) {
		await fs.ensureDir(dir);
	}

	await fs.ensureDir(ORIGIN_CHROME_PATH);

	await fs.copy(ORIGIN_CHROME_PATH, RELEASE_CHROME_PATH);
	await fs.copy(ORIGIN_CONFIG_PATH, RELEASE_CONFIG_PATH);
	await fs.copy(ORIGIN_CSV_PATH, RELEASE_CSV_PATH);
	await fs.copy(ORIGIN_DB_PATH, RELEASE_DB_PATH);

	return await exec([
		'--target', ['node16-win'].join(','),
		'--output', path.join(RELEASE_PATH, 'release'),
		'--compress', 'GZip',
		'--public', path.join(__dirname, '../index.js'),
	]);
};

exports.Builder = Builder;